/**
 * 
 */
package vista;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * @author De La
 *
 */
@SuppressWarnings("serial")
public class MenuCreditos extends JPanel {
	
	private JTextArea creditos = new JTextArea();
	private JButton volver = new JButton("Volver");
	private JPanel principal;
	private Fondo fondo = new Fondo( "marron.gif" );
	private JPanel panel = this;
	
	public MenuCreditos(JPanel principal) {
		this.principal = principal;
		setLayout(null);
		
		//Configuro los creditos		
		creditos.setFont(new Font("Serif", Font.PLAIN, 12));
		creditos.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Creditos"),
                                BorderFactory.createEmptyBorder(5,5,5,5)),
                creditos.getBorder()));
		try{
			creditos.setText(readFileAsString("creditos.txt"));
		}
		catch (IOException lala){
			System.out.println("allooo");
		}
		creditos.setBounds(30,20,200,250);
		creditos.setVisible(true);
		creditos.setEditable(false);
		add(creditos);
		
		//Configuro el boton volver
		volver.setBounds(30,280,100,20);
		volver.addActionListener(this.getListenerVolver());
		add(volver);
	}
	
	private static String readFileAsString(String filePath)
    throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
	
	public ActionListener getListenerVolver() {
		return new EscuchaBotonVolver();
	}
	
	private class EscuchaBotonVolver implements ActionListener
	{	public void actionPerformed(ActionEvent e)
		{
			panel.getParent().remove( panel );
			principal.setVisible(true);
		}
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		fondo.paintComponent(g,getWidth(),getHeight());
	}
}
